<template>
  <views class="agent_list">
    <viewTitle :title="'代理商列表'" :border="true"></viewTitle>
    <div class="search_list">
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="flex flexR center">
          <div class="inputW flex flexR center">
            <el-form-item label="代理商名称:">
              <el-input type="text" clearable placeholder="代理商名称" v-model="agent_name"></el-input>
            </el-form-item>
          </div>
          <div class="inputW flex flexR center">
            <el-form-item label="所属服务商:">
              <el-select v-model="facilitator_id" placeholder="所属服务商" filterable clearable>
                <el-option
                  v-for="item in facilitator_select_list"
                  :key="item.facilitator_id"
                  :label="item.facilitator_name"
                  :value="item.facilitator_id"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="inputW flex flexR center">
            <el-form-item label="状态:">
              <el-select v-model="status" placeholder="状态" clearable>
                <el-option
                  v-for="item in agent_status_select"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <el-form ref="form" label-width="auto" :inline="true">
        <div class="flex flexR center">
          <div class="inputW flex flexR center">
            <el-form-item label="代理商编号:">
              <el-input type="text" clearable placeholder="代理商编号" v-model="agent_sn"></el-input>
            </el-form-item>
          </div>
          <el-form-item label="提交时间:">
            <el-date-picker
              v-model="agent_created_time"
              value-format="timestamp"
              :default-time="['0:00:00','23:59:59']"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              align="right"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <div class="search_unit">
              <el-button
                type="primary"
                :style="{width:140+'px'}"
                @click="exportExcel"
                size="medium"
                icon="el-icon-download"
              >导出列表</el-button>
            </div>
            <div class="search_unit">
              <el-button @click="reset()" icon="el-icon-refresh">重置</el-button>
            </div>
            <div class="search_unit">
              <el-button type="primary" @click="query()" icon="el-icon-search">查询</el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="table_data">
      <el-table
        :data="tableData"
        border
        style="width: 100%;max-width:none;"
        :header-cell-style="getRowClass"
        :select-on-indeterminate="true"
        :fit="true"
        :lazy="true"
        @row-click="clickRow"
        max-height="450"
      >
        <el-table-column fixed="left" type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="agent_sn" label="代理商编号" width="150"></el-table-column>
        <el-table-column prop="agent_name" label="代理商名称" width="120"></el-table-column>
        <el-table-column prop="agent_user" label="初始管理员账号" width="120"></el-table-column>
        <el-table-column prop="facilitator_name" label="所属服务商" width="120"></el-table-column>
        <el-table-column prop="lead_agent_name" label="上级代理" width="120"></el-table-column>
        <el-table-column prop="contact_name" label="联系人" width="120"></el-table-column>
        <el-table-column prop="contact_mobile" label="手机号码" width="150"></el-table-column>
        <el-table-column prop="agent_status" label="状态"></el-table-column>
        <el-table-column prop="created_datetime" label="创建时间" width="180"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150" v-if="isLook">
          <template slot-scope="scope">
            <el-button type="primary" @click="look(scope.row.agent_id)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="paging flex flexR">
        <el-pagination
          style="background:#fff; padding:10px 0"
          background
          layout="total, sizes,prev, pager, next, jumper"
          @size-change="handleSizeChange($event)"
          @current-change="changePage($event)"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.page_size"
          :total="pagination.total_count"
          :current-page="pagination.current_page"
          :page-count="pagination.page_count"
        ></el-pagination>
      </div>
    </div>
  </views>
</template>





<script>
export default {
  name: "AgentList",
  data() {
    return {
      agent_status_select: [
        //列表中，企业可选择状态
        {
          label: "待审核",
          value: 0
        },
        {
          label: "审核通过",
          value: 1
        },
        {
          label: "审核不通过",
          value: 2
        },
        {
          label: "已冻结",
          value: 3
        }
      ],
      pickerOptions: {
        //企业创建时间快捷方式选择插件
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      facilitator_select_list: [], //平台可选服务商列表
      facilitator_id: "", //选择的服务商ID
      status: "", //列表中选中的代理商状态
      agent_created_time: [], //企业创建时间范围
      agent_sn: "", //代理商编号
      agent_name: "", //代理商名称
      tableData: [], //表格数据
      download_url: "",
      pagination: {
        //分页数据
        page_start: 0, //搜索开始页码
        current_page: 0, //当前页码
        page_count: 0, //总页数
        page_size: 10, //每页数量
        total_count: 0 //总数量
      }
    };
  },
  mounted() {
    //获取平台可选服务商简易列表
    this.getSimpleFacilitatorList();

    //获取列表数据
    this.query();
  },
  computed: {
    //实时计算要传输的编辑参数
    post_data() {
      let data = {};
      if (this.agent_created_time && this.agent_created_time.length > 0) {
        data.agent_created_time_start = this.agent_created_time[0]/1000;
        data.agent_created_time_end = this.agent_created_time[1]/1000;
      }
      data.agent_sn = this.agent_sn;
      data.agent_name = this.agent_name;
      data.status = this.status;
      data.facilitator_id = this.facilitator_id;
      data.pagination = this.pagination;
      return this.$filterParam(data);
    },
    isLook(){      //判断是否具有 “查看” 按钮权限
      return  this.$store.state.buttonPermission[this.$route.path]  &&  this.$store.state.buttonPermission[this.$route.path].look ? 1 : 0;
    }
  },
  methods: {
    //    修改第一行样式
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #FAFAFA ";
      } else {
        return "";
      }
    },
    //     点击单行
    clickRow(row, column, event) {
      console.log(row, column, event);
    },
    //    修改是否换行
    changeStyleRow(width) {
      let _width = width - 290 - 350 - 300 - 50 - 25;
      if (_width > 300) {
        this.marginLeft = 25;
      } else {
        this.marginLeft = 0;
      }
    },
    getSimpleFacilitatorList() {
      this.$raxios
        .post(this.$mapapi.getSimpleFacilitator, {})
        .then(res => {
          if (res.code != 200) return Promise.reject(res.msg);
          this.facilitator_select_list = res.data;
        })
        .catch(res => {
          this.$message.error(res);
        });
    },
    handleSizeChange(page_size) {
      //变动每页数量时
      this.pagination.page_size = page_size;
      this.query();
    },
    changePage(current_page) {
      //切换页码时
      this.pagination.page_start = current_page;
      this.query();
    },
    //导出
    exportExcel() {
      this.$import_excal(this.download_url || "");
    },
    query() {
      //获取列表数据
      this.$raxios
        .post(this.$mapapi.getAgentList, { data: this.post_data })
        .then(res => {
          if (res.code != 200 && res.code != 501)
            return Promise.reject(res.msg);
          this.tableData = res.data.list;
          this.pagination = res.data.pagination;
          this.download_url = res.data.download_url || "";
          console.log("user_list", res);
        })
        .catch(res => {
          this.$message.error(res);
        });
    },
    look(agent_id) {
      this.$toRoutePath({
        path: "/agent_list/agent_list_look",
        edit: true,
        query: { agent_id }
      });
    },
    reset() {
        this.agent_sn = "";
        this.agent_name = "";
        this.status = "";
        this.facilitator_id = "";
        this.agent_created_time = [];
        this.query();
    }
  }
};
</script>


<style  lang="less" :scope="true" src="@/style/agent/agent_list.less">
</style>
